var Generator = require('yeoman-generator')
var path = require('path')
var fs = require('fs')
function resolve(dir) {
  return path.join(__dirname, dir)
}

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms))
}
module.exports = class extends (
  Generator
) {
  constructor(args, opts) {
    // Calling the super constructor is important so our generator is correctly set up
    super(args, opts)
    this.argument('appname', {
      type: String,
      required: false,
      default: 'uniinit',
    })
    this.argument('t', { type: String, required: false })
    // This method adds support for a `--babel` flag
    this.option('babel')
  }
  initializing() {
    this.log('当前路径:', this.destinationRoot())
    this.log('模板路径：', this.templatePath())
    this.log(this.config.getAll())
  }
  async prompting() {
    this.answers = await this.prompt([
      {
        type: 'input',
        name: 'name',
        message: 'Your project name',
        default: this.appname,
        store: true,
      },
      {
        type: 'confirm',
        name: 'cool',
        message: 'Would you like to enable the Cool feature?',
      },
    ])
  }
  configuring() {}
  default() {}
  async writing() {
    this.fs.copyTpl(
      this.templatePath('index.html'),
      this.destinationPath('index.html'),
      {
        title: 'Templating with Yeoman',
      }
    )
    let files = []
    let fileDisplay = (filePath) => {
      //根据文件路径读取文件，返回文件列表
      fs.readdir(filePath, function (err, files) {
        if (err) {
          console.warn(err)
        } else {
          //遍历读取到的文件列表
          files.forEach(function (filename) {
            //获取当前文件的绝对路径
            var filedir = path.join(filePath, filename)
            //根据文件路径获取文件信息，返回一个fs.Stats对象
            fs.stat(filedir, function (eror, stats) {
              if (eror) {
                console.warn('获取文件stats失败')
              } else {
                var isFile = stats.isFile() //是文件
                var isDir = stats.isDirectory() //是文件夹
                if (isFile) {
                  files.push(filedir)
                  console.log(files)
                }
                if (isDir) {
                  fileDisplay(filedir) //递归，如果是文件夹，就继续遍历该文件夹下面的文件
                }
              }
            })
          })
        }
      })
    }
    fileDisplay(this.templatePath())
    await sleep(3000)
    this.files = files
    this.log(this.files)
    files.map((v) => {
      this.log(v, this.destinationPath(v.replace(this.templatePath(), '')))
      this.fs.copyTpl(
        v,
        this.destinationPath(v.replace(this.templatePath(), '')),
        { title: 'Templating with Yeoman' }
      )
    })
  }
  install() {
    // npm
    // this.npmInstall(['lodash'], { 'save-dev': true });
    // yarn
    // this.yarnInstall(['lodash'], { dev: true })
    // cmd
    this.spawnCommand('echo', ['cmd command'])
  }
  end() {
    // this.log('cool feature2', this.answers.cool)
    this.log('end')
  }
}
